package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.common.utils.security.Md5Utils;
import com.ruoyi.system.domain.Payroll;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.domain.SysUserPost;
import com.ruoyi.system.mapper.PayrollMapper;
import com.ruoyi.system.mapper.SysPostMapper;
import com.ruoyi.system.service.ISysPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SalaryStandardMapper;
import com.ruoyi.system.domain.SalaryStandard;
import com.ruoyi.system.service.ISalaryStandardService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 工资标准Service业务层处理
 * 
 * @author mgy
 * @date 2022-11-13
 */
@Service
public class SalaryStandardServiceImpl extends ServiceImpl<SalaryStandardMapper, SalaryStandard> implements ISalaryStandardService
{
    @Autowired
    private SalaryStandardMapper salaryStandardMapper;
    @Autowired
    private SysPostMapper postMapper;

    /**
     * 查询工资标准
     * 
     * @param id 工资标准主键
     * @return 工资标准
     */
    @Override
    public SalaryStandard selectSalaryStandardById(Long id)
    {
        return salaryStandardMapper.selectSalaryStandardById(id);
    }

    /**
     * 查询工资标准列表
     * 
     * @param salaryStandard 工资标准
     * @return 工资标准
     */
    @Override
    public List<SalaryStandard> selectSalaryStandardList(SalaryStandard salaryStandard)
    {
        return salaryStandardMapper.selectSalaryStandardList(salaryStandard);
    }

    /**
     * 新增工资标准
     * 
     * @param salaryStandard 工资标准
     * @return 结果
     */
    @Override
    public int insertSalaryStandard(SalaryStandard salaryStandard)
    {
        String userName = ShiroUtils.getSysUser().getUserName();
        Date nowDate = DateUtils.getNowDate();
        salaryStandard.setCreateTime(nowDate);
        salaryStandard.setUpdateTime(nowDate);
        salaryStandard.setUpdateBy(userName);
        salaryStandard.setCreateBy(userName);

        return salaryStandardMapper.insertSalaryStandard(salaryStandard);
    }
    /**
     * 导入工资标准数据
     *
     * @param list 工资标准数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作工资标准
     * @return 结果
     */
    @Override
    public String importSalaryStandard(List<SalaryStandard> list, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(list) || list.size() == 0)
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();

        for (SalaryStandard salaryStandard : list)
        {
            try
            {
                //根据导入数据查询表中是否存在岗位工资标准信息
                int count = count(new LambdaQueryWrapper<SalaryStandard>().eq(SalaryStandard::getPostName, salaryStandard.getPostName()));
                //查询岗位是否存在
                SysPost post = postMapper.selectPostByName(salaryStandard.getPostName());
                // 如果岗位存在，且表中没有岗位工资标准
                if (post!=null&&count==0)
                {
                    String userName = ShiroUtils.getSysUser().getUserName();
                    Date nowDate = DateUtils.getNowDate();
                    salaryStandard.setCreateTime(nowDate);
                    salaryStandard.setUpdateTime(nowDate);
                    salaryStandard.setUpdateBy(userName);
                    salaryStandard.setCreateBy(userName);
                    salaryStandard.setPostCode(post.getPostCode());
                    this.insertSalaryStandard(salaryStandard);
                    successNum++;
                    successMsg.append("<br/>" + failureNum + "、岗位 " + salaryStandard.getPostName() + " 的工资标准导入成功");
                }
                else if (isUpdateSupport)
                {
                    String userName = ShiroUtils.getSysUser().getUserName();
                    Date nowDate = DateUtils.getNowDate();
                    salaryStandard.setUpdateTime(nowDate);
                    salaryStandard.setUpdateBy(userName);
                    salaryStandard.setPostCode(post.getPostCode());
                    this.updateSalaryStandard(salaryStandard);
                    successNum++;
                    successMsg.append("<br/>" + failureNum + "、岗位 " + salaryStandard.getPostName() + " 的工资标准更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、岗位 " + salaryStandard.getPostName() + " 的工资标准已存在或不存在此岗位");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、岗位 " + salaryStandard.getPostName() + " 的工资标准导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    /**
     * 修改工资标准
     * 
     * @param salaryStandard 工资标准
     * @return 结果
     */
    @Override
    public int updateSalaryStandard(SalaryStandard salaryStandard)
    {
        String userName = ShiroUtils.getSysUser().getUserName();
        salaryStandard.setUpdateBy(userName);
        salaryStandard.setUpdateTime(DateUtils.getNowDate());
        return salaryStandardMapper.updateSalaryStandard(salaryStandard);
    }

    /**
     * 批量删除工资标准
     * 
     * @param ids 需要删除的工资标准主键
     * @return 结果
     */
    @Override
    public int deleteSalaryStandardByIds(String ids)
    {
        return salaryStandardMapper.deleteSalaryStandardByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除工资标准信息
     * 
     * @param id 工资标准主键
     * @return 结果
     */
    @Override
    public int deleteSalaryStandardById(Long id)
    {
        return salaryStandardMapper.deleteSalaryStandardById(id);
    }
}
